package com.rocket.bedroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.spring.util.ByteUtils;
import com.rocket.bedroom.pojo.LoginEntity;
import com.rocket.bedroom.pojo.PositionEntity;
import com.rocket.bedroom.service.ILoginService;
import com.rocket.bedroom.service.IPositionService;
import com.rocket.bedroom.service.impl.LoginServiceImpl;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.Assert;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.Authenticator;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 20:21 2020/6/6
 * @ModifyBy:
 */
@Component
public class LoginRealm extends AuthorizingRealm
{

    @Resource
    private ILoginService loginService;

    @Resource
    private IPositionService positionService;


    //授权方法：AuthorizingRealm
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
    {
        //principalCollection == Subject.getPrincipal()当前主体对象
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        if (null != principalCollection)
        {
            //subject本体
            LoginEntity loginEntity = (LoginEntity) principalCollection.getPrimaryPrincipal();
            LambdaQueryWrapper<PositionEntity> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.eq(PositionEntity::getPositionId, loginEntity.getPositionId());
            PositionEntity loginPosition = positionService.getOne(lambdaQuery);

            simpleAuthorizationInfo.addRole(loginPosition.getPositionName());
        }
        //授权角色信息


        return simpleAuthorizationInfo;
    }

    //登录认证:AuthenticatingRealm
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException
    {
        //可自定义连接数据库，完成用户登录认证
        String phone = (String) authenticationToken.getPrincipal();//获取用户名
        LoginEntity loginByPhone = loginService.findLoginByPhone(phone);
        Assert.notNull(loginByPhone, "login phone is null");
        if (phone.equals(loginByPhone.getPhone()))
        {
            //参数3是Realm的名字，系统中会自定义调用父类getName方法即可
            SimpleAuthenticationInfo simpleAuthenticationInfo =
                    new SimpleAuthenticationInfo(
                            loginByPhone,
                            loginByPhone.getLoginPassword(),
                            ByteSource.Util.bytes(loginByPhone.getSalt()),
                            this.getName());
            return simpleAuthenticationInfo;//返回用户认证成功后的认证信息
        }
        return null;
    }

       /*public static void main(String[] args)
    {
        //source：需要加密的字符

        //普通md5加密
        Md5Hash hash = new Md5Hash("123456");
        System.out.println("md5：" + hash.toHex());

        //md5+salt
        Md5Hash m5s = new Md5Hash("123456", "*&*74");
        System.out.println("md5+salt：" + m5s.toHex());

        //md5+salt+hash散列
        Md5Hash msh = new Md5Hash("123456", "*&&1", 1024);
        System.out.println("md5+salt+hash散列：" + msh.toHex());


    }*/

}
